﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Net;
using System.Data;
using System.IO;
using System.Text;

// http://localhost:88/ws/Sample/WebMethod/PriceHistoryWebService.asmx?WSDL
/// <summary>
/// Summary description for PriceHistoryWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PriceHistoryWebService : System.Web.Services.WebService {

    public PriceHistoryWebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public XmlDocument getPriceHistoryAsXML(string strTicker, string strStartDate, string strEndDate)
    {
        XmlDocument xReturn = new XmlDocument();
        HelperFunctions hlp = new HelperFunctions();
        xReturn = hlp.getXML(strTicker, strStartDate, strEndDate);
        return xReturn;
    }
    
}

public class HelperFunctions
{

    public string BuildYahooURI(string strTicker, string strStartDate, string strEndDate)
    {
        string strReturn = "";
        DateTime dStart = Convert.ToDateTime(strStartDate);
        DateTime dEnd = Convert.ToDateTime(strEndDate);
        string sStartDay = dStart.Day.ToString();
        string sStartMonth = (dStart.Month - 1).ToString();
        string sStartYear = dStart.Year.ToString();
        string sEndDay = dEnd.Day.ToString();
        string sEndMonth = (dEnd.Month - 1).ToString();
        string sEndYear = dEnd.Year.ToString();
        StringBuilder sYahooURI = new StringBuilder("http://ichart.finance.yahoo.com/table.csv?s=");
        sYahooURI.Append(strTicker); sYahooURI.Append("&a=");
        sYahooURI.Append(sStartMonth); sYahooURI.Append("&b=");
        sYahooURI.Append(sStartDay);

        sYahooURI.Append("&c=");
        sYahooURI.Append(sStartYear);
        sYahooURI.Append("&d=");
        sYahooURI.Append(sEndMonth);
        sYahooURI.Append("&e=");
        sYahooURI.Append(sEndDay);
        sYahooURI.Append("&f=");
        sYahooURI.Append(sEndYear);
        sYahooURI.Append("&g=d");
        sYahooURI.Append("&ignore=.csv");
        strReturn = sYahooURI.ToString();

        return strReturn;
    }

    public XmlDocument getXML(string strTicker, string strStartDate, string strEndDate)
    {
        XmlDocument xReturn = new XmlDocument();
        DataSet result = new DataSet();
        string sYahooURI = BuildYahooURI(strTicker, strStartDate, strEndDate);
        WebClient wc = new WebClient();
        Stream yData = wc.OpenRead(sYahooURI);
        result = GenerateDataSet(yData);
        StringWriter stringWriter = new StringWriter();
        XmlTextWriter xmlTextwriter = new XmlTextWriter(stringWriter);
        result.WriteXml(xmlTextwriter, XmlWriteMode.WriteSchema);
        XmlNode xRoot = xReturn.CreateElement("root");
        xReturn.AppendChild(xRoot);
        xReturn.LoadXml(stringWriter.ToString());
        return xReturn;
    }

    public DataSet GenerateDataSet(Stream yData)
    {
        DataSet result = new DataSet();
        StreamReader sRead = new StreamReader(yData);
        string[] columns = sRead.ReadLine().Split(',');
        result.Tables.Add("TimeSeries");
        foreach (string col in columns)
        {
            // Удаляем пробелы из имен 
            string thiscol = col.Replace(" ", "");
            // Добавляем имя столбца 
            result.Tables["TimeSeries"].Columns.Add(thiscol);
        }
        string sData = sRead.ReadToEnd();
        string[] rows = sData.Split('\n');
        foreach (string row in rows)
        {
            string[] items = row.Split(',');
            result.Tables["TimeSeries"].Rows.Add(items);
        }

        return result;
    }

}

